package com.huiyeji.hui.controller;

import com.huiyeji.hui.auth.Authenticator;
import com.huiyeji.hui.auth.License;
import com.huiyeji.hui.entity.User;
import com.huiyeji.hui.service.ApplicationService;
import com.huiyeji.hui.utils.HuiResponse;
import com.huiyeji.hui.utils.SuccessEnum;
import com.huiyeji.hui.vo.InvitationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author zhaoduo
 */
@RestController
@RequestMapping("/invitation")
public class InvitationController {

    private final ApplicationService service;

    private final Authenticator authenticator = Authenticator.getInstance();

    @Autowired
    public InvitationController(ApplicationService service) {
        this.service = service;
    }

    @GetMapping("/getInvitations")
    public HuiResponse<?> getInvitations(HttpSession session){
        User user = authenticator.check(session, License.GENERAL);
        List<InvitationVo> vos = service.getInvitations(user.getUid());
        return HuiResponse.ok(vos);
    }

    @PostMapping("/refuseInvitation/{id}")
    public HuiResponse<?> refuseInvitation(@PathVariable ("id")Long id,
                                           HttpSession session){
        User user = authenticator.check(session, License.GENERAL);
        service.refuseInvitation(user.getUid(),id);
        return HuiResponse.ok(SuccessEnum.REFUSE_INVITATION);
    }

    @PostMapping("/agreeInvitation/{id}")
    public HuiResponse<?> agreeInvitation(@PathVariable ("id")Long id,
                                           HttpSession session){
        User user = authenticator.check(session, License.GENERAL);
        service.agreeInvitation(user.getUid(),id);
        return HuiResponse.ok(SuccessEnum.AGREE_INVITATION);
    }

    @DeleteMapping("/delete/{id}")
    public HuiResponse<?> deleteInvitation(@PathVariable("id") Long id,
                                           HttpSession session){
        User user = authenticator.check(session, License.GENERAL);
        service.deleteInvitation(user.getUid(),id);
        return HuiResponse.ok(SuccessEnum.DELETE_SUCCESS);
    }
}
